using Autofac;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Plugin.DiscountRules.Customers.Data;
using Nop.Plugin.DiscountRules.Customers.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Plugin.DiscountRules.Customers
{
	public class DependencyRegistrar : IDependencyRegistrar
	{
		public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
		{
			const string serviceName = "nop_object_context_discountrules_customer";
			//Register services
			builder.RegisterType<RequirementCustomerService>().As<IRequirementCustomerService>();

			var dataSettingsManager = new DataSettingsManager();
			var dataProviderSettings = dataSettingsManager.LoadSettings();

			if (dataProviderSettings != null && dataProviderSettings.IsValid())
			{
				//register named context
				builder.Register<IDbContext>(c => new DiscountCustomerObjectContext(dataProviderSettings.DataConnectionString))
					.Named<IDbContext>(serviceName)
					.InstancePerLifetimeScope();

				builder.Register<DiscountCustomerObjectContext>(c => new DiscountCustomerObjectContext(dataProviderSettings.DataConnectionString))
					.InstancePerLifetimeScope();
			}
			else
			{
				//Register entity context
				builder.Register<IDbContext>(c => new DiscountCustomerObjectContext(c.Resolve<DataSettings>().DataConnectionString))
					.Named<IDbContext>(serviceName)
					.InstancePerLifetimeScope();
				builder.Register<DiscountCustomerObjectContext>(c => new DiscountCustomerObjectContext(c.Resolve<DataSettings>().DataConnectionString))
					.InstancePerLifetimeScope();
			}
		}


		public int Order
		{
			get { return 0; }
		}
	}
}
